package com.group.neps.controller;


import com.group.common.users.NEPSUser;
import com.group.common.utils.MyUtils;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

import java.io.IOException;
import java.util.ArrayList;

public class NepsLoginController {
    @FXML
    private Button btnLogin;
    @FXML
    private Button btnRegister;
    @FXML
    private TextField txtPhoneNumber;
    @FXML
    private PasswordField txtPassWord;

    public void login(ActionEvent  event) throws IOException, ClassNotFoundException {
        String filePath = "group/data/neps_userdata_arraylist.dat";
        ArrayList<NEPSUser> userArrayList = MyUtils.readFileToArrayList(filePath);
        for(NEPSUser u : userArrayList) {
            if(u.getPhoneNumber().equals(txtPhoneNumber.getText()) && u.getPassword().equals(txtPassWord.getText())) {
                FeedbackProvideViewController.setUser(u);
                MyUtils.showAlert("登录成功", "登录成功", "欢迎您，" + u.getUsername() + "！", Alert.AlertType.INFORMATION);
                MyUtils.navigateToView(event, "group/neps/view/FeedbackProvideView.fxml");
                return;
            }
        }
        MyUtils.showAlert("登录失败", "登录失败", "用户名或密码错误！", Alert.AlertType.ERROR);
    }

    public void register() {
        //跳转到用户注册界面
        MyUtils.showView((Stage) btnRegister.getScene().getWindow(),"group/neps/view/NepsRegisterView.fxml");
    }
    @FXML
    public void initialize() {
        //TODO
    }
}
